package com.anxin.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.anxin.dao.BaseDAO;
import com.anxin.util.PageListData;

public class BaseDAOImpl<T, PK extends Serializable> extends
		SqlSessionDaoSupport implements BaseDAO<T, PK> {

	public static Logger logger = LoggerFactory.getLogger(BaseDAOImpl.class);
	@Autowired(required = true)
	@Resource(name = "sqlSessionFactory")
	public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	// 保存
	public T save(T entity) {
		try {
			getSqlSessionTemplate().insert(
					entity.getClass().getSimpleName() + ".save", entity);
			return entity;
		} catch (RuntimeException re) {
			logger.error("save " + entity.getClass().getName() + " failed :{}",
					re);
			throw re;
		}
	}
	// 更新
	public void update(T entity) {
		try {
			this.getSqlSessionTemplate().update(
					entity.getClass().getSimpleName() + ".update", entity);
		} catch (RuntimeException re) {
			logger.error("update " + entity.getClass().getName()
					+ " failed :{}", re);
			throw re;
		}
	}
	// 删除
	public void delete(T entity) {
		try {
			this.getSqlSessionTemplate().delete(
					entity.getClass().getSimpleName() + ".delete", entity);
		} catch (RuntimeException re) {
			logger.error("delete " + entity.getClass().getName()
					+ " failed :{}", re);
			throw re;
		}
	}

	// 根据id删除某个对象
	public void delete(Class<T> entityClass, PK id) {
		try {
			this.getSqlSessionTemplate().delete(
					entityClass.getSimpleName() + ".deleteById", id);
		} catch (RuntimeException re) {
			logger.error("delete " + entityClass.getName() + "failed :{}", re);
			throw re;
		}
	}
	// 根据id加载某个对象
	public T findById(Class<T> entityClass, PK id) {
		try {
			return (T) this.getSqlSessionTemplate().selectOne(
					entityClass.getSimpleName() + ".findById", id);
		} catch (RuntimeException re) {
			logger.error("findById " + entityClass.getName() + "failed :{}",
					re);
			throw re;
		}
	}

	// 查找所有的对象
	public List<T> findAll(Class<T> entityClass) {
		try {
			return this.getSqlSessionTemplate().selectList(
					entityClass.getSimpleName() + ".findAll");
		} catch (RuntimeException re) {
			logger
					.error("findAll " + entityClass.getName() + "failed :{}",
							re);
			throw re;
		}
	}
	// 根据查询参数，当前页数，每页显示的数目得到分页列表
	public PageListData queryPage(Class<T> entityClass, Map param,
			int currentPage, int pageSize) {
		try {
			return new PageListData((Integer)getSqlSessionTemplate()
					.selectOne(entityClass.getSimpleName()+".getTotalCounts",param), pageSize, currentPage, this
					.getSqlSessionTemplate().selectList(
							entityClass.getSimpleName() + ".queryPage", param));
		} catch (RuntimeException re) {
			logger.error("findList " + entityClass.getName() + "failed :{}",
					re);
			throw re;
		}
	}
}